﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

// Created libraries
using FrbaBus.Core;
namespace FrbaBus.Compra_de_Pasajes
{

    public partial class Cliente : Form
    {
        public Cliente()
        {
            InitializeComponent();
        }
        private SeleccionarButacas parent = null;
        private SeleccionarEncomiendas parent2 = null;
        private RealizarPago parent3 = null;
        private int modificacion = 0;
        private int sexomod = 0;
        public Cliente (Form callingForm)
        {
            if (string.Compare(callingForm.Name, "SeleccionarButacas") == 0)
            parent = callingForm as SeleccionarButacas;
            if (string.Compare(callingForm.Name, "SeleccionarEncomiendas") == 0)
                parent2 = callingForm as SeleccionarEncomiendas;
            if (string.Compare(callingForm.Name, "RealizarPago") == 0)
                parent3 = callingForm as RealizarPago;


            InitializeComponent();
        }


        private void Cliente_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (textBox1.Text == "")
            {
                MessageBox.Show("Debe ingresar su número de documento");
            }
            else
            {
                //Chequeo la existencia del DNI en la base
                DBManager db = new DBManager();
                SqlCommand consulta = new SqlCommand("select ALL_IN.existeCliente('" + textBox1.Text + "')");
                SQLResponse r = db.executeQuery(consulta);
                if (r.result.Rows[0][0].ToString() == "True")
                {
                    SqlCommand consulta2 = new SqlCommand("select * from ALL_IN.Cliente where DNI = '" + textBox1.Text + "'");
                    SQLResponse r2 = db.executeQuery(consulta2);
                    textBox2.Text = r2.result.Rows[0][1].ToString();
                    textBox3.Text = r2.result.Rows[0][2].ToString();
                    textBox4.Text = r2.result.Rows[0][3].ToString();
                    textBox5.Text = r2.result.Rows[0][4].ToString();
                    textBox6.Text = r2.result.Rows[0][5].ToString();
                    textBox7.Text = r2.result.Rows[0][6].ToString();
                    if (r2.result.Rows[0][7].ToString() == "True")
                    {
                        checkBox2.Checked = true;
                        checkBox2.Enabled = false;
                    }
                    if (r2.result.Rows[0][8].ToString() == "X")
                    {
                        sexomod = 1;
                        MessageBox.Show("El campo Sexo no esta especificado, por favor completelo.");
                        groupBox3.Enabled = true;
                    }
                    else if (r2.result.Rows[0][8].ToString() == "F")
                    {
                        radioButton2.Checked = true;
                    }
                    else if (r2.result.Rows[0][8].ToString() == "M")
                    {
                        radioButton1.Checked = true;
                    }

                }
                else
                {
                    MessageBox.Show("Introdujo un DNI incorrecto, o que no esta dado de alta como cliente existente.");
                }
                db.desconectar();

            }
        }

        private void textchanged(object sender, KeyPressEventArgs e)
        {
            int i = e.KeyChar;
            if (i >= 48 && i <= 57 || i == 8 || i == 13)
            {
            }
            else
            {
                MessageBox.Show("Solo están permitidos caracteres numéricos.");
                e.Handled = true;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            groupBox3.Enabled = true;
            textBox2.ReadOnly = false;
            textBox3.ReadOnly = false;
            textBox4.ReadOnly = false;
            textBox5.ReadOnly = false;
            textBox6.ReadOnly = false;
            checkBox2.Enabled = true;
            modificacion = 1;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                textBox1.ReadOnly = true;
                textBox8.Enabled = true;
                textBox8.Text = "";
                label6.Visible = false;
                textBox7.Visible = false;
                label8.Visible = true;
                label9.Visible = true;
                label10.Visible = true;
                label11.Visible = true;
                textBox9.Visible = true;
                comboBox1.Visible = true;
                comboBox2.Visible = true;

            }
            else
            {
                label8.Visible = false;
                label9.Visible = false;
                label10.Visible = false;
                label11.Visible = false;
                textBox9.Visible = false;
                comboBox1.Visible = false;
                comboBox2.Visible = false;
                label6.Visible = true;
                textBox7.Visible = true;
                textBox1.ReadOnly = false;
                textBox2.ReadOnly = true;
                textBox3.ReadOnly = true;
                textBox4.ReadOnly = true;
                textBox5.ReadOnly = true;
                textBox6.ReadOnly = true;
                textBox7.ReadOnly = true;
                textBox8.Enabled = false;
                textBox8.Text = "";
                label7.Enabled = false;
                checkBox2.Enabled = false;
                checkBox2.Checked = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DBManager db = new DBManager();
            if (checkBox1.Checked == false) //Update del cliente ya existente
            {
                if (textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "" || textBox7.Text == "" || (radioButton1.Checked == false && radioButton2.Checked == false))
                {
                    MessageBox.Show("Debe completar todos los campos.");
                }
                else//TODO: Ver de agregar fecha de salida como modificable.
                {
                    int discapacidad = 0;
                    int jubi = 0;
                    string sexo = "F";
                    if (checkBox2.Checked == true)
                    {
                        discapacidad = 1;
                    }
                    if (radioButton1.Checked == true)
                    {
                        sexo = "M";
                    }
                    if (modificacion == 1)// Con modificaciones
                    {
                       SqlCommand consulta = new SqlCommand("UPDATE ALL_IN.Cliente set Nombre='" + textBox2.Text + "',Apellido='" + textBox3.Text + "',Direccion='" + textBox4.Text + "',Telefono='" + textBox5.Text + "',mail='" + textBox6.Text + "',Discapacidad='" + discapacidad + "',Sexo='" + sexo +"' where DNI=" + textBox1.Text);
                        db.executeQuery(consulta);
                        MessageBox.Show("Se han modificados sus datos!");
                    }
                    else if (sexomod == 1)
                    {
                        SqlCommand consulta = new SqlCommand("UPDATE ALL_IN.Cliente set Sexo='" + sexo + "' where DNI=" + textBox1.Text);
                        db.executeQuery(consulta);
                    }
                    
                    db.desconectar();
                    if (parent != null)
                    {
                        string[] words = textBox7.Text.ToString().Split('/');
                        string[] words2 = words[2].Split(' ');
                        string dia1 = words[0];
                        string mes1 = words[1];
                        string anio1 = words2[0];
                        int nacimiento = int.Parse(anio1);
                        int ahora = int.Parse((Convert.ToDateTime(System.Configuration.ConfigurationManager.AppSettings["Fecha"])).ToString("yyyy"));

                        if (sexo == "M")
                        {
                            if ((ahora - nacimiento) >= 60)
                            {
                                jubi = 1;
                            }
                        }
                        else
                        {
                            if ((ahora - nacimiento) >= 65)
                            {
                                jubi = 1;
                            }
                        }
                        parent.dataGridView1.SelectedCells[5].Value = string.Copy(textBox1.Text.ToString());
                        parent.dataGridView1.SelectedCells[6].Value = string.Copy(discapacidad.ToString());
                        parent.dataGridView1.SelectedCells[7].Value = string.Copy(jubi.ToString());
                    }
                    else if (parent2 != null)
                    {
                        parent2.textBox1.Text = string.Copy(textBox1.Text.ToString());
                    }
                    else if( parent3!= null)
                    {
                        parent3.textBox1.Text = string.Copy(textBox1.Text.ToString());
                    }
                     
                    this.Close();
                }
            }
            else //Nuevo Cliente
            {
                if (textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "" || textBox8.Text == "" || comboBox1.Text == "" || comboBox2.Text == "" || (radioButton1.Checked == false && radioButton2.Checked == false))
                {
                    MessageBox.Show("Debe completar todos los campos.");
                }
                else
                {
                    string fecha = textBox9.Text + "-" + comboBox1.Text + "-" + comboBox2.Text + " 00:00";
                    int jubi = 0;
                    int discapacidad = 0;
                    string sexo = "F";
                    if (checkBox2.Checked == true)
                    {
                        discapacidad = 1;
                    }
                    if (radioButton1.Checked == true)
                    {
                        sexo = "M";
                    }

                    SqlCommand consulta5 = new SqlCommand("INSERT INTO ALL_IN.Cliente values(" + textBox8.Text + ",'" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + fecha + "','" + discapacidad + "','" + sexo + "')");
                    db.executeQuery(consulta5);
                    MessageBox.Show("Se ha dado de alta como nuevo cliente!");
                    db.desconectar();

                    if (parent != null)
                    {
                        int nacimiento = int.Parse(textBox9.Text);
                        int ahora = int.Parse((Convert.ToDateTime(System.Configuration.ConfigurationManager.AppSettings["Fecha"])).ToString("yyyy"));
                        if (sexo == "M")
                        {
                            if ((ahora - nacimiento) >= 60)
                            {
                                jubi = 1;
                            }
                        }
                        else
                        {
                            if ((ahora - nacimiento) >= 65)
                            {
                                jubi = 1;
                            }
                        }
                        parent.dataGridView1.SelectedCells[7].Value = string.Copy(jubi.ToString());
                        parent.dataGridView1.SelectedCells[5].Value = string.Copy(textBox8.Text.ToString());
                        parent.dataGridView1.SelectedCells[6].Value = string.Copy(discapacidad.ToString());
                    }
                    else if (parent2 != null)
                    {
                        parent2.textBox1.Text = string.Copy(textBox8.Text.ToString());
                    }
                    else if (parent3 != null)
                    {
                        parent3.textBox1.Text = string.Copy(textBox8.Text.ToString());
                    }
                    this.Close();
                }
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox8.Text == "")
            {
                MessageBox.Show("Debe colocar un numero de DNI.");
            }
            else
            {
                DBManager db = new DBManager();
                SqlCommand consulta2 = new SqlCommand("select ALL_IN.existeCliente('" + textBox8.Text + "')");
                SQLResponse r = db.executeQuery(consulta2);
                if (r.result.Rows[0][0].ToString() == "True")
                {
                    MessageBox.Show("El DNI especificado ya es un dni existente. Intente nuevamente.");
                    textBox8.Text = "";
                }
                else
                {
                    textBox2.ReadOnly = false;
                    textBox2.Text = "";
                    textBox3.Text = "";
                    textBox4.Text = "";
                    textBox5.Text = "";
                    textBox6.Text = "";
                    textBox7.Text = "";
                    textBox3.ReadOnly = false;
                    textBox4.ReadOnly = false;
                    textBox5.ReadOnly = false;
                    textBox6.ReadOnly = false;
                    textBox7.ReadOnly = false;
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox3.Text = "";
                    textBox4.Text = "";
                    textBox5.Text = "";
                    textBox6.Text = "";
                    textBox7.Text = "";
                    checkBox2.Enabled = true;
                    checkBox2.Checked = false;
                }
            }

        }
    }
}


 